﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;

using ExtAspNet;

namespace WebSite.admin
{
    public partial class index : BaseAdmin
    {

        readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.ValidateLoginRule();
                this.BindContent();
                BindTreeList();
            }
        }

        void BindTreeList()
        {

            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);
            if (oCompany != null)
            {
                base.CompanyId = oCompany.Id.ToString();


                DataSet dsCompany;
                dsCompany = theUserRuleRef.GetListByAdminIdForCompany(iAdminId);
                if (dsCompany != null && dsCompany.Tables.Count > 0)
                {

                    foreach (DataRow dr in dsCompany.Tables[0].Rows)
                    {
                        ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["CompanyRule"].ToString(), true);
                        if (!string.IsNullOrEmpty(base.CompanyId) && base.CompanyLevelId == "4" && dr["RuleDescription"].ToString() == "添加商家") continue;
                        BindTreeNode(dr["ParentId"].ToString(), dr["ParentDescription"].ToString(), dr["CompanyRule"].ToString(), dr["RuleDescription"].ToString(), dr["Url"].ToString());
                    }
                }


                //BindTreeNode("CardManager", "卡片管理", "OpenCardManger", "开卡管理", "product/OpenCard.aspx");
                //BindTreeNode("CompanyManger", "商家管理", "AddCompany", "添加商家", "company/companyManager.aspx");
                //BindTreeNode("CompanyManger", "公司管理", "CompanyList", "商家列表", "company/companyList.aspx");
            }
            else
            {
                DataSet ds;
                ds = theUserRuleRef.GetListByAdminId(iAdminId);
                if (ds != null && ds.Tables.Count > 0)
                {

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                        BindTreeNode(dr["ParentId"].ToString(), dr["ParentDescription"].ToString(), dr["Name"].ToString(), dr["Description"].ToString(), dr["Url"].ToString());
                    }
                }
            }
        }


        void BindTreeNode(string parentId, string parentName, string subId, string subName, string url)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                if (parentName == "商品管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/010.gif";
                }
                else if (parentName == "订单管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/019.gif";
                }
                else if (parentName == "卡片管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/042.gif";
                }
                else if (parentName == "文章管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/141.gif";
                }
                else if (parentName == "系统设置")
                {
                    node.IconUrl = "/admin/images/xp_icons/159.gif";
                }
                else if (parentName == "商家管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/203.gif";
                }
                else if (parentName == "管理员设置")
                {
                    node.IconUrl = "/admin/images/xp_icons/07.gif";
                }
                else if (parentName == "报表统计")
                {
                    node.IconUrl = "/admin/images/xp_icons/281.gif";
                }
                else if (parentName == "会员管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/17.gif";
                }
                else if (parentName == "发行商管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/097.gif";
                }
                else if (parentName == "反馈建议管理")
                {
                    node.IconUrl = "/admin/images/xp_icons/237.gif";
                }
               
               
                this.treeMenu.Nodes.Add(node);
                nodeIndex = FindNodeById(parentId);
            }

            if (!string.IsNullOrEmpty(url))
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.SingleClickExpand = true;
                node.NavigateUrl = url;
                node.IconUrl = "/admin/images/Editor/table_cf.gif";
                node.Text = subName;
                this.treeMenu.Nodes[nodeIndex].Nodes.Add(node);
            }

        }

        int  FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeMenu.Nodes.Count; i++)
            {
                if (this.treeMenu.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;
         
         }

    

        // Methods
        private void BindContent()
        {
            //this.hlkNewOrder.Text = count.counts("", "DB_order", "states", "0");
            //this.hlkProcess.Text = count.counts("", "DB_order", "states", "1");
            //this.hlkOut.Text = count.counts("", "DB_order", "states", "2");
            //this.hlkEnd.Text = count.counts("", "DB_order", "states", "3");
            //   this.Label5.Text = count.counts("", "DB_order", "states", "3");
            //this.goods1.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "0");
            //this.goods2.Text = count.countsb("", "DB_Shoppro", "visible", "1", "protype", "1");
            //this.goods3.Text = count.counts("", "DB_Shoppro", "proisnew1", "1");
            //this.goods4.Text = count.counts("", "DB_Shoppro", "proisnew2", "1");
            //this.goods5.Text = count.counts("", "DB_Shoppro", "proisnew3", "1");
            //this.goods6.Text = count.counts("", "DB_Shoppro", "iscuxao", "1");
         // this.msg.Text = count.counts("", "DB_Msg", "1", "1");
           // this.comment.Text = count.counts("", "DB_Comment", "1", "1");
        }

        void ValidateLoginRule()
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == ERule.OrderProcess)
                    {

                      //  this.extFormOrder.Hidden = false;
                        this.BindContent();
                        return;
                    }

                }
            }


        }
        protected void ImageButton6_Click(object sender, ImageClickEventArgs e)
        {
            base.Session["Adminlogin"] = null;
            base.Session["Uid"] = null;
            base.Response.Write("<script language=javascript>window.top.location.href='login.aspx';</script>");
            base.Response.End();
        }

        protected void Window2_Close(object sender, WindowCloseEventArgs e)
        {
            ExtAspNet.Alert.Show("修改成功！");
        }
  
    }
}